package com.teng.smartdrawer.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.teng.smartdrawer.R;
import com.teng.smartdrawer.bean.Component;
import com.teng.smartdrawer.utils.ImageLoadingConfig;

import java.util.List;

/**
 * Created by teng on 16/5/10.
 */
public class DetailAdapter extends BaseAdapter {

    private List<Component> mData;
    private Context context;

    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions imgOptions = ImageLoadingConfig.generateDisplayImageOptions(R.mipmap.icon);

    /**
     * 当某一个条目通过 CheckBox选中发生变化，回调的接口
     *
     */
    private CompoundButton.OnCheckedChangeListener itemCheckedListener;

    private View.OnClickListener onCountChangeListener;

    private int littleCount;

    public DetailAdapter(List<Component> mData, Context context) {
        this.mData = mData;
        this.context = context;
    }

    public CompoundButton.OnCheckedChangeListener getItemCheckedListener() {
        return itemCheckedListener;
    }

    public void setItemCheckedListener(CompoundButton.OnCheckedChangeListener itemCheckedListener) {
        this.itemCheckedListener = itemCheckedListener;
    }

    public void setOnCountChangeListener(View.OnClickListener onCountChangeListener) {
        this.onCountChangeListener = onCountChangeListener;
    }



    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View ret = null;

        if (ret != null) {
            ret = convertView;
        }else {
            ret = LayoutInflater.from(context).inflate(R.layout.activity_part_adapter , parent , false);
        }

        ViewHolder viewHolder = (ViewHolder) ret.getTag();

        if (viewHolder == null) {
            viewHolder = new ViewHolder();
            viewHolder.iv_item_icon = (ImageView) ret.findViewById(R.id.iv_item_icon);
            viewHolder.reduce = (ImageView) ret.findViewById(R.id.reduce);
            viewHolder.reduce.setOnClickListener(onCountChangeListener);
            viewHolder.add = (ImageView) ret.findViewById(R.id.add);
            viewHolder.add.setOnClickListener(onCountChangeListener);

            viewHolder.tv_item_content = (TextView) ret.findViewById(R.id.tv_item_content);
            viewHolder.tv_item_name = (TextView) ret.findViewById(R.id.tv_item_name);

            viewHolder.put = (TextView) ret.findViewById(R.id.dt_put);
            viewHolder.put.setOnClickListener(onCountChangeListener);

            viewHolder.get = (TextView) ret.findViewById(R.id.dt_get);
            viewHolder.get.setOnClickListener(onCountChangeListener);

            viewHolder.dt_number = (TextView) ret.findViewById(R.id.dt_number);

            viewHolder.get_com = (TextView) ret.findViewById(R.id.get_com);
            viewHolder.get_com.setOnClickListener(onCountChangeListener);

            viewHolder.checkBox = (CheckBox) ret.findViewById(R.id.checkbox);
            viewHolder.checkBox.setOnCheckedChangeListener(itemCheckedListener);

            ret.setTag(viewHolder);
        }

        Component component = mData.get(position);

        String m_imgpc = component.getM_imgpc();

        imageLoader.displayImage(m_imgpc , viewHolder.iv_item_icon , imgOptions);

        String m_fixed_num = component.getM_fixed_num();
        int limit_number = Integer.parseInt(m_fixed_num);

        if (limit_number < component.getM_number()){
            viewHolder.tv_item_content.setTextColor(Color.BLUE);
        }else {
            viewHolder.tv_item_content.setTextColor(Color.RED);
        }

        viewHolder.tv_item_content.setText(component.getM_number()+"");

        viewHolder.tv_item_name.setText(component.getM_name() + "");

        int count = component.getCount();

        int need_num = component.getM_neednum();

        if (recommendType == 0) {
            viewHolder.dt_number.setText(count + "");

            viewHolder.get_com.setVisibility(View.GONE);
            viewHolder.put.setVisibility(View.VISIBLE);
            viewHolder.get.setVisibility(View.VISIBLE);

        }else {
            // 模块
            component.setCount(need_num);
            viewHolder.dt_number.setText(need_num + "");
            viewHolder.get_com.setVisibility(View.VISIBLE);
            viewHolder.put.setVisibility(View.GONE);
            viewHolder.get.setVisibility(View.GONE);

            if (need_num > component.getM_number()){
                viewHolder.get_com.setText("短 缺");
                viewHolder.get_com.setTextColor(Color.RED);
                isCanOpenAllDrawer = false;
            }else {
                viewHolder.get_com.setText("取 出");
                viewHolder.get_com.setTextColor(Color.WHITE);
                viewHolder.get_com.setBackgroundResource(R.drawable.button_bg);
            }

        }

        if (count == 0){
            viewHolder.reduce.setEnabled(false);
        }else {
            viewHolder.reduce.setEnabled(true);
        }

        if(listMode == 1){
            // 显示选中框
            viewHolder.checkBox.setVisibility(View.VISIBLE);
            viewHolder.checkBox.setChecked(component.isChecked());

        }else {

            viewHolder.checkBox.setVisibility(View.GONE);
            viewHolder.checkBox.setChecked(false);

            for (Component c : mData) {
                c.setChecked(false);
            }
        }

        viewHolder.add.setTag(position);
        viewHolder.reduce.setTag(position);
        viewHolder.put.setTag(position);
        viewHolder.get.setTag(position);
        viewHolder.checkBox.setTag(position);
        viewHolder.get_com.setTag(position);

        return ret;
    }


    int listMode;
    // 当是模块的详情时，没有存放，只有取出
    int recommendType;

    boolean isCanOpenAllDrawer = true;

    public boolean isCanOpenAllDrawer() {
        return isCanOpenAllDrawer;
    }

    public void setIsCanOpenAllDrawer(boolean isCanOpenAllDrawer) {
        this.isCanOpenAllDrawer = isCanOpenAllDrawer;
    }

    public int getRecommendType() {
        return recommendType;
    }

    public void setRecommendType(int recommendType) {
        this.recommendType = recommendType;
    }

    public void switchEditMode(){
        if (listMode == 1)
            listMode = 0;
        else if (listMode == 0)
            listMode = 1;

        notifyDataSetChanged();
    }

    public void setEditMode(int editMode){
        listMode = editMode;
    }

    public int getEditMode(){
        return listMode;
    }

    private static class ViewHolder{

        public ImageView iv_item_icon , reduce , add;

        public TextView tv_item_name , tv_item_content , dt_number ;

        public TextView put , get;

        private CheckBox checkBox;

        private TextView get_com;
    }

}
